//
//  JGCollectViewController.m
//  JGMeiTuanHD
//
//  Created by JJetGu on 15/9/20.
//  Copyright (c) 2015年 Free. All rights reserved.
//

#import "JGCollectViewController.h"
#import "JGConst.h"
#import "JGDealDBTool.h"
#import "JGDeal.h"
#import "MJRefresh.h"

@interface JGCollectViewController ()

@property (nonatomic, assign) int currentPage;

@end

@implementation JGCollectViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的收藏";

    // 加载第一页的收藏数据
    [self loadMoreDeals];
    
    // 监听收藏状态改变的通知
    [JGNotificationCenter addObserver:self selector:@selector(collectStateChange:) name:JGCollectStateDidChangeNotification object:nil];
    
    // 添加上啦加载
    [self.collectionView addFooterWithTarget:self action:@selector(loadMoreDeals)];

}

#pragma mark - 加载收藏数据
- (void)loadMoreDeals
{
    // 1.增加页码
    self.currentPage++;
    
    // 2.增加新数据
    [self.deals addObjectsFromArray:[JGDealDBTool collectDeals:self.currentPage]];
    
    // 3.刷新表格
    [self.collectionView reloadData];
    
    // 4.结束刷新
    [self.collectionView footerEndRefreshing];
}


#pragma mark - 通知监听
- (void)collectStateChange:(NSNotification *)notification
{
    [self.deals removeAllObjects];
    
    self.currentPage = 0;
    [self loadMoreDeals];
}

/**
 *  删除
 */
- (void)remove
{
    for (JGDeal *deal in self.willDeletedDeals) {
        [JGDealDBTool removeCollectDeal:deal];
    }
}


#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    // 控制尾部控件的显示和隐藏
    self.collectionView.footerHidden = (self.deals.count == [JGDealDBTool collectDealsCount]);

    return [super collectionView:collectionView numberOfItemsInSection:section];
}

#pragma mark - 实现父类方法
- (NSString *)emptyIcon
{
    return @"icon_collects_empty";
}
@end
